/**
 * 
 */
package cn.g31010.sip.service.impl;

import java.util.List;

import org.apache.log4j.Logger;

import cn.g31010.sip.pojo.User;
import cn.g31010.sip.service.AuthService;
import cn.g31010.sip.service.LoginService;
import cn.g31010.sip.service.UserManageService;
import cn.g31010.sip.util.DB;
import cn.g31010.sip.util.IDUtil;

import com.db4o.ObjectSet;
import com.db4o.query.Predicate;
import com.gqf.common.util.DynamicResource;

/**
 * @author user
 * 
 */
public class UserManageServiceImpl implements UserManageService, LoginService,
		AuthService {
	private DB db = DB.getDB(DynamicResource.getString("appchat", "db.file"));
	private Logger logger = Logger.getLogger(UserManageServiceImpl.class);
	private IDUtil uid= IDUtil.getIntance("cn.g31010.sip.service.impl.uid");
	
	public UserManageServiceImpl() {
		uid.setPattern("UID00000");
	}

	public User login(String id, String pwd) {
		return null;
	}

	public void logout(String id) {

	}

	@SuppressWarnings("unchecked")
	public User auth(String name, String passwd) {
		logger.debug("auth(" + name + "," + passwd + ")");
		User user = findUserByName(name);
		logger.debug("findUserByName:" + user);
		if (user != null
				&& (user.getPasswd() == passwd || passwd == null ? user
						.getPasswd().equals(passwd) : passwd.equals(user
						.getPasswd()))) {
			
			logger.debug("return:" + user);
			return user;
		}
		logger.debug("return: null");
		return null;
	}

	public User addUser(User user) {
		if(this.findUserByName(user.getName())!=null)
			return null;
		logger.debug("addUser(" + user + ")");
		user.setRegisterDate(System.currentTimeMillis());
		user.setStatus("offline");
		user.setId(uid.genId());
		db.add(user);
		logger.debug("return:" + user);
		return user;
	}

	public User findUserById(String id) {
		final String uid = id;
		logger.debug("findUserById(" + id + ")");
		Predicate<User> filter = new Predicate<User>() {
			public boolean match(User candidate) {
				logger.debug("match(" + candidate + ")");
				return candidate.getId() == uid || uid == null ? candidate
						.getId().equals(uid) : uid.equals(candidate.getId());
			}
		};
		ObjectSet<User> rs = db.find(filter);
		logger.debug("db.find(filter):" + rs.size());
		User user = null;
		if (rs.size() > 0) {
			user = rs.get(0);
		}
		logger.debug("rs.get(0):" + user);
		logger.debug("return:" + user);
		return user;
	}

	public User findUserByName(String name) {
		final String uname = name;
		logger.debug("findUserByName(" + name + ")");
		Predicate<User> filter = new Predicate<User>() {
			public boolean match(User candidate) {
				logger.debug("match(" + candidate + ")");
				return candidate.getName() == uname || uname == null ? candidate
						.getName().equals(uname)
						: uname.equals(candidate.getName());
			}
		};
		ObjectSet<User> rs = db.find(filter);
		logger.debug("db.find(filter):" + rs.size());
		User user = null;
		if (rs.size() > 0) {
			user = rs.get(0);
		}
		logger.debug("rs.get(0):" + user);
		logger.debug("return:" + user);
		return user;
	}

	public List<User> findUserByNickName(String nickName) {
		final String nnick = nickName;
		logger.debug("findUserByNickName(" + nickName + ")");
		Predicate<User> filter = new Predicate<User>() {
			public boolean match(User candidate) {
				logger.debug("match(" + candidate + ")");
				return candidate.getNickName() == nnick || nnick == null ? candidate
						.getNickName().equals(nnick)
						: nnick.equals(candidate.getNickName());
			}
		};
		ObjectSet<User> rs = db.find(filter);
		logger.debug("db.find(filter):" + rs.size());
		logger.debug("return: List<User>(" + rs.size() + ")");
		return rs.subList(0, rs.size());
	}

	public List<User> getAllUser() {
		logger.debug("getAllUser()");
		ObjectSet<User> rs = db.find(User.class);
		logger.debug("db.find(User.class):" + rs.size());
		logger.debug("return: List<User>(" + rs.size() + ")");
		return rs.subList(0, rs.size());
	}

	public User removeUser(String id) {
		logger.debug("removeUser(" + id + ")");
		User user = this.findUserById(id);
		logger.debug("findUserById(id):" + user);
		if (user != null) {
			db.delete(user);
			logger.debug("db.delete(" + user + ")");
		}
		logger.debug("return:" + user);
		return user;
	}

	public User setUser(User user) {
		logger.debug("setUser(" + user + ")");
		User u = null;
		if (user != null) {
			u = this.findUserById(user.getId());
			logger.debug("findUserById(" + user.getId() + "):" + u);
			if (u != null) {
				u.setNickName(user.getNickName());
				u.setPasswd(user.getPasswd());
				db.update(u);
				logger.debug("db.update(" + user + ")");
			}
		}
		logger.debug("return:" + u);
		return u;
	}

}
